#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int m[3]={23,28,33};
int a[3];
int exgcd(int a,int b,int &x,int &y){
    if(!b){
        x=1;
        y=0;
        return a;
    }
    int d=exgcd(b,a%b,x,y);
    int t=x;
    x=y;
    y=t-a/b*y;
    return d;
}
int crt(int n){
    int M=1;
    int ans=0;
    for(int i=0;i<n;i++){
        M*=m[i];
    }
    for(int i=0;i<n;i++){
        int x,y;
        int Mi=M/m[i];
        exgcd(Mi,m[i],x,y);
        ans=(ans+Mi*x*a[i])%M;
    }
    if(ans<0){
        ans+=M;
    }
    return ans;
}
int main(void){
    int n;
    scanf("%d",&n);
    int p,e,i,d;
    int c=1;
    while(~scanf("%d%d%d%d",&a[0],&a[1],&a[2],&d)){
        if(a[0]==-1 && a[1]==-1 && a[2]==-1 && d==-1){
            break;
        }
        int ans=crt(3);
        ans-=d;
        if(ans<=0){
            //21252是0 0 0 0的值，也就是从第0天开始到三峰的时间周期
            ans+=21252;
        }
        printf("Case %d: the next triple peak occurs in %d days.\n",c++,ans);
    }
    return 0;
}